package com.dongbao.core.config;

import com.dongbao.common.AuthorityEnum;
import com.dongbao.util.StorageConfig;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.util.xmlb.XmlSerializerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static com.dongbao.common.AuthorityEnum.AUTHORITY_NOTIFICATION_TITLE;

@State(name = "dhg-authority",storages = {
        @Storage(value = "dhgAuthority.xml")
})
public class MyState implements PersistentStateComponent<MyState> {

    private StorageConfig config;

    public static MyState getInstance(){
        return ServiceManager.getService(MyState.class);
    }

    @Nullable
    @Override
    public MyState getState() {
        return this;
    }

    @Override
    public void loadState(@NotNull MyState myState) {
        if (null == myState) {
            return;
        }
        XmlSerializerUtil.copyBean(myState, this);
    }

    public StorageConfig getConfig() {
        if (null == config) {
            config = new StorageConfig();
            config.setCrmUrlPath("http://crm.dongbaosoft.com:8092");
            config.setGitUrlPath("http://192.168.2.220");
            config.setMqUrlPath("tcp://192.168.2.182:1883");
            config.setIpScope("2");
            config.setCachePath("D:\\\\plugin_cache");
        }
        if(config.getCrmUrlPath()==null){
            config.setCrmUrlPath("http://crm.dongbaosoft.com:8092");
        }
        if(config.getGitUrlPath()==null){
            config.setGitUrlPath("http://192.168.2.220");
        }
        if(config.getMqUrlPath()==null){
            config.setMqUrlPath("tcp://192.168.2.182:1883");
        }
        if(config.getIpScope()==null){
            config.setIpScope("2");
        }
        if(config.getCachePath()==null){
            config.setCachePath("D:\\\\plugin_cache");
        }
        if(config.getDaoFileCheckTableNames()==null){
            String[][] daoFileCheckTableNames = {
                {"","员工档案","emp_info","1"},
                {"","组织档案","per_orgs","1"},
                {"","模块管理","sys_module","0"},
            };
            config.setDaoFileCheckTableNames(daoFileCheckTableNames);
        }
        return config;
    }

    public void setConfig(StorageConfig config) {
        this.config = config;
    }
}